package com.serkol.driving.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.bean.MyPage;
import com.serkol.driving.learning.entity.LoginLog;
import com.serkol.driving.learning.mapper.LoginLogMapper;
import com.serkol.driving.learning.service.LoginLogService;
import com.serkol.modal.sql.SqlFeildModal;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author pingan
* @description 针对表【login_log】的数据库操作Service实现
* @createDate 2022-09-16 19:47:38
*/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog>
    implements LoginLogService{


  //  @Cacheable(cacheNames = "loginlog", key = "#token")
    @Override
    public LoginLog isLogin(String token) {
        QueryWrapper<LoginLog> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(LoginLog::getToken,token).orderByDesc(LoginLog::getId).eq(LoginLog::getState,0);


        queryWrapper.last("limit 1");
        LoginLog one = this.getOne(queryWrapper);
        if(one!=null){
            return  one;
        }else{
            return null;
        }

    }

    @Override
    public MyPage findByList(int pageSize,int current) {
        QueryWrapper<LoginLog> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("ID");
        Page<LoginLog> page=new Page<>(current,pageSize);
        IPage<LoginLog> loginLogPage = this.baseMapper.selectPage(page, queryWrapper);
        return MyPage.parsePage(loginLogPage);
    }

    @Override
    public List<Map<String, Object>> loginChartStastic() {
        Date date = new Date();
   /*     MyLambdaQueryWrapper<UsersWalletLog> queryWrapper = new MyLambdaQueryWrapper<>();
//dateFormat(UsersWalletLog::getCreateDate,DateFormat.DATE_MONTH).as("dateName")
        queryWrapper.sum(UsersWalletLog::getPrice).as("price").dateFormat(UsersWalletLog::getCreateDate, DateFormat.DATE_MONTH).as("name").notIn(UsersWalletLog::getStatus, 2).
                eq(UsersWalletLog::getState, 1);
        queryWrapper.last(" GROUP BY name ORDER BY price DESC");*/
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getLoginLogStatastica());
        queryWrapper.groupBy("ymd").orderByAsc("name");
       // queryWrapper.groupBy("name").orderByDesc("name");
        List<Map<String, Object>> maps = this.listMaps(queryWrapper);
        return maps;
    }
    public static String getLoginLogStatastica() {
        String sql = "date_format(CREATE_DATE,'%Y-%m-%d') as ymd,date_format(CREATE_DATE,'%Y-%m') as ym,date_format(CREATE_DATE,'%d') as name,COUNT(*) as count ";
        return sql;
    }
}




